var timer = null;

// document.getElementById("modalBg").addEventListener("click", function(event) {
//   event.stopPropagation(); // 阻止冒泡
// });

function openModals() {

  // 获取弹窗元素并打开弹窗  
  var modal = document.getElementById("modal");
  var qrcode = document.getElementById("qrcode");


  $.ajax({
    url: window.location.origin + "/index/wanlshop/entry/addEarnestOrder",
    type: "POST",
    dataType: "json",
    data: $(this).serialize(),
    success: function (ret) {
      console.log(ret)
      if (ret.code == 1) {
        qrcode.src = ret.img;
      } else {
        console.log(ret)                                
      }
    },
    error: function (xhr) {
      console.log(xhr)
    }
  });
  modal.style.display = 'block';
  setTimeout(() => {
    modal.style.opacity = '1';
    // RefreshTime()
  }, 500);
}  

function RefreshTime() {
  // 刷新倒计时
  var countdown = document.getElementById("countdown");
  var qrcode = document.getElementById("qrcode");  
  var seconds = 60;
  timer = setInterval(() => {
    countdown.innerHTML = "支付倒计时：" + seconds + "s";
    if (seconds == 0) {
      clearInterval(timer);
      countdown.innerHTML = "二维码已过期";
      qrcode.src = 'https://img1.baidu.com/it/u=666927474,3753237121&fm=253&fmt=auto&app=138&f=GIF?w=504&h=500';
    } else { 
      seconds--;
    }
  }, 1000);
}
  
function closeModal() {  
  // 获取弹窗元素并关闭弹窗  
  var modal = document.getElementById("modal");  
  modal.style.opacity = '0';
  setTimeout(() => {
    modal.style.display = 'none';
    clearInterval(timer);
  }, 500);
}

function confirm() {  
  // 确认已付款并请求接口检测
  closeModal();
}